package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.ElectromagneticPulseModel;
import com.aysafety.iotdataplatformserver.dao.ElectromagneticPulseDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

// 电磁脉冲
@Component
public class ElectromagneticPulseListener {

    private final String type = "ElectromagneticPulse";

    private final String tableName = "ElectromagneticPulseRealTime";

    private final String[] structure = new String[]{"ElectromagneticPulse"};

    private Logger logger = LoggerFactory.getLogger(ErrorListener.class);

    @Autowired
    KsTreeDataListener ksTreeDataListener;

    @Autowired
    ElectromagneticPulseDao electromagneticPulseDao;

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public void start(Map<String, Object> result) {
        try {
            String clientId = result.get("clientId").toString();
            if (!result.get("dataType").toString().equals("histroy")) {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    ElectromagneticPulseModel electromagneticPulse = this.transformData(listData);
                    this.save(electromagneticPulse, clientId);
                }
            } else {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    ElectromagneticPulseModel electromagneticPulse = this.transformData(listData);
                    this.save(electromagneticPulse, clientId);
                }
            }
        } catch (IOException e) {
            logger.info("ElectromagneticPulseListener:", e);
        }
    }

    public boolean save(ElectromagneticPulseModel electromagneticPulse, String clientId) throws IOException {
        String timestamp = String.valueOf(electromagneticPulse.getDatetime().getTime());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId", electromagneticPulse.getEntId() + "." + "ElectromagneticPulseRealTime");
        map.put("pk", "\'" + timestamp +".:." + electromagneticPulse.getDeviceId() + "\'");
        map.put("clientId", "\'" + clientId + "\'");
        map.put("deviceId", "\'" + electromagneticPulse.getDeviceId() + "\'");
        map.put("entId", "\'" + electromagneticPulse.getEntId() + "\'");
        map.put("unit", "\'" + electromagneticPulse.getUnit() + "\'");
        map.put("workingFace", "\'" + electromagneticPulse.getWorkingFace() + "\'");
        map.put("datetime", electromagneticPulse.getDatetime().getTime());
        map.put("monitoringValue", electromagneticPulse.getMonitoringValue());
        map.put("poklad", electromagneticPulse.getPoklad());
        map.put("smallestRange", electromagneticPulse.getSmallestRange());
        map.put("maximumRange", electromagneticPulse.getMaximumRange());
        map.put("stationName", "\'" + electromagneticPulse.getStationName() + "\'");
        map.put("status", electromagneticPulse.isStatus());
        map.put("powerCutArea", "\'" + electromagneticPulse.getPowerCutArea() + "\'");
        map.put("powerReconnectionLimit", electromagneticPulse.getPowerReconnectionLimit());
        map.put("powerCutLimit", electromagneticPulse.getPowerCutLimit());
        electromagneticPulseDao.insertElectromagneticPulseRealTime(map);
        ksTreeDataListener.generateTreeData(electromagneticPulse.getEntId(), this.type,
            electromagneticPulse.getWorkingFace(), electromagneticPulse.getDeviceId(),
            electromagneticPulse.getDatetime(), "");
        return true;
    }

    public ElectromagneticPulseModel transformData(Map<String, Object> result) {
        ElectromagneticPulseModel electromagneticPulse = new ElectromagneticPulseModel();
        if (null != result.get("entId")) {
            electromagneticPulse.setEntId(result.get("entId").toString());
        } else {
            electromagneticPulse.setEntId("无数据");
        }
        if (null != result.get("deviceId")) {
            electromagneticPulse.setDeviceId(result.get("deviceId").toString());
        } else {
            electromagneticPulse.setDeviceId("无数据");
        }
        electromagneticPulse.setMonitoringValue(Double.valueOf(result.get("monitoringValue").toString()));
        if (null != result.get("poklad")) {
            electromagneticPulse.setPoklad(Double.valueOf(result.get("poklad").toString()));
        } else {
            electromagneticPulse.setPoklad(-1.0);
        }
        electromagneticPulse.setDatetime(DateUtils.fomatDateTime(Long.valueOf(result.get("datetime").toString())));
        if (null != result.get("workingFace")) {
            electromagneticPulse.setWorkingFace(result.get("workingFace").toString());
        } else {
            electromagneticPulse.setWorkingFace("无数据");
        }
        if (null != result.get("unit")) {
            electromagneticPulse.setUnit(result.get("unit").toString());
        } else {
            electromagneticPulse.setUnit("无数据");
        }
        if (null != result.get("smallestRange")) {
            electromagneticPulse.setSmallestRange(Double.valueOf(result.get("smallestRange").toString()));
        } else {
            electromagneticPulse.setSmallestRange(-1.0);
        }
        if (null != result.get("maximumRange")) {
            electromagneticPulse.setMaximumRange(Double.valueOf(result.get("maximumRange").toString()));
        } else {
            electromagneticPulse.setSmallestRange(-1.0);
        }
        if (null != result.get("stationName")) {
            electromagneticPulse.setStationName(result.get("stationName").toString());
        } else {
            electromagneticPulse.setUnit("无数据");
        }
        if (null != result.get("status")) {
            if (result.get("status").toString().equals("1")) {
                electromagneticPulse.setStatus(TRUE);
            } else {
                electromagneticPulse.setStatus(FALSE);
            }
        } else {
            electromagneticPulse.setStatus(FALSE);
        }
        if (null != result.get("powerCutArea")) {
            electromagneticPulse.setPowerCutArea(result.get("powerCutArea").toString());
        } else {
            electromagneticPulse.setPowerCutArea("无数据");
        }
        if (null != result.get("powerReconnectionLimit")) {
            electromagneticPulse
                    .setPowerReconnectionLimit(Double.valueOf(result.get("powerReconnectionLimit").toString()));
        } else {
            electromagneticPulse.setPowerReconnectionLimit(-1.0);
        }
        if (null != result.get("powerCutLimit")) {
            electromagneticPulse.setPowerCutLimit(Double.valueOf(result.get("powerCutLimit").toString()));
        } else {
            electromagneticPulse.setPowerCutLimit(-1.0);
        }
        return electromagneticPulse;
    }
}
